<template>
  <div class="login-container">
    <!-- 背景装饰 -->
    <div class="background-decoration">
      <div class="circle circle-1"></div>
      <div class="circle circle-2"></div>
      <div class="circle circle-3"></div>
    </div>
    
    <div class="login-content">
      <!-- 左侧装饰区域 -->
      <div class="login-left">
        <div class="brand-section">
          <div class="logo">
            <i class="el-icon-lock"></i>
          </div>
          <h1 class="brand-title">RBAC权限控制系统</h1>
          <p class="brand-subtitle">安全、高效、智能的权限管理平台</p>
        </div>
        <div class="features">
          <div class="feature-item">
            <i class="el-icon-check"></i>
            <span>多级权限控制</span>
          </div>
          <div class="feature-item">
            <i class="el-icon-check"></i>
            <span>角色灵活配置</span>
          </div>
          <div class="feature-item">
            <i class="el-icon-check"></i>
            <span>安全可靠</span>
          </div>
        </div>
      </div>
      
      <!-- 右侧登录表单 -->
      <div class="login-right">
        <div class="login-form-container">
          <div class="form-header">
            <h2>欢迎回来</h2>
            <p>请登录您的账户</p>
          </div>
          
          <el-form
            ref="loginFormRef"
            :model="loginForm"
            :rules="loginRules"
            class="login-form"
            @submit.prevent="handleLogin"
          >
            <el-form-item prop="username">
              <el-input
                v-model="loginForm.username"
                placeholder="请输入用户名"
                prefix-icon="User"
                size="large"
                class="custom-input"
              />
            </el-form-item>
            
            <el-form-item prop="password">
              <el-input
                v-model="loginForm.password"
                type="password"
                placeholder="请输入密码"
                prefix-icon="Lock"
                size="large"
                show-password
                class="custom-input"
              />
            </el-form-item>
            
            <el-form-item prop="captcha">
              <div class="captcha-container">
                <el-input
                  v-model="loginForm.captcha"
                  placeholder="请输入验证码"
                  prefix-icon="Key"
                  size="large"
                  class="custom-input captcha-input"
                />
                <div class="captcha-image-container">
                  <img
                    v-if="captchaImageUrl"
                    :src="captchaImageUrl"
                    alt="验证码"
                    class="captcha-image"
                    @click="refreshCaptcha"
                  />
                  <el-button
                    v-else
                    type="default"
                    size="large"
                    class="captcha-button"
                    @click="refreshCaptcha"
                  >
                    获取验证码
                  </el-button>
                </div>
              </div>
            </el-form-item>
            
            <el-form-item>
              <div class="form-options">
                <el-checkbox v-model="loginForm.rememberMe" class="remember-checkbox">
                  记住我
                </el-checkbox>
              </div>
            </el-form-item>
            
            <el-form-item>
              <el-button
                type="primary"
                size="large"
                class="login-button"
                :loading="loading"
                @click="handleLogin"
              >
                <i class="el-icon-right"></i>
                登录
              </el-button>
            </el-form-item>
          </el-form>
          
          <div class="form-footer">
            <!-- 删除演示账号信息 -->
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import { ElMessage } from 'element-plus'
import { captchaApi } from '@/api/captcha'

const router = useRouter()
const authStore = useAuthStore()

const loginFormRef = ref()
const loading = ref(false)
const captchaImageUrl = ref('')

const loginForm = reactive({
  username: '',
  password: '',
  captcha: '',
  rememberMe: false
})

const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ],
  captcha: [
    { required: true, message: '请输入验证码', trigger: 'blur' }
  ]
}

// 保存登录信息到localStorage
const saveLoginInfo = (username, password, rememberMe) => {
  if (rememberMe) {
    localStorage.setItem('savedUsername', username)
    localStorage.setItem('savedPassword', password)
    localStorage.setItem('savedRememberMe', 'true')
  } else {
    localStorage.removeItem('savedUsername')
    localStorage.removeItem('savedPassword')
    localStorage.removeItem('savedRememberMe')
  }
}

// 从localStorage恢复登录信息
const restoreLoginInfo = () => {
  const savedRememberMe = localStorage.getItem('savedRememberMe')
  
  if (savedRememberMe === 'true') {
    const savedUsername = localStorage.getItem('savedUsername')
    const savedPassword = localStorage.getItem('savedPassword')
    
    if (savedUsername) {
      loginForm.username = savedUsername
    }
    if (savedPassword) {
      loginForm.password = savedPassword
    }
    loginForm.rememberMe = true
  }
}

// 刷新验证码
const refreshCaptcha = async () => {
  try {
    const response = await captchaApi.getCaptchaImage()
    
    if (response.data) {
      const blob = new Blob([response.data], { type: 'image/png' })
      captchaImageUrl.value = URL.createObjectURL(blob)
      loginForm.captcha = '' // 清空验证码输入
    } else {
      throw new Error('验证码响应数据为空')
    }
  } catch (error) {
    ElMessage.error(`获取验证码失败: ${error.message}`)
  }
}

const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  try {
    await loginFormRef.value.validate()
    loading.value = true
    
    // 登录前强制清理之前的权限状态
    authStore.clearAuthData()
    
    const result = await authStore.login(loginForm)
    
    if (result.success) {
      // 保存登录信息
      saveLoginInfo(loginForm.username, loginForm.password, loginForm.rememberMe)
      
      ElMessage.success('登录成功')
      router.push('/')
    } else {
      ElMessage.error(result.message)
      // 登录失败时刷新验证码
      refreshCaptcha()
    }
  } catch (error) {
    // authStore已经处理了错误信息，这里只需要处理意外错误
    ElMessage.error('登录过程中发生意外错误')
    refreshCaptcha()
  } finally {
    loading.value = false
  }
}

// 组件挂载时获取验证码和恢复登录信息
onMounted(() => {
  refreshCaptcha()
  restoreLoginInfo()
})
</script>

<style scoped>
.login-container {
  height: 100vh;
  background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
}

/* 背景装饰 */
.background-decoration {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.circle {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  animation: float 6s ease-in-out infinite;
}

.circle-1 {
  width: 200px;
  height: 200px;
  top: -100px;
  right: -100px;
  animation-delay: 0s;
}

.circle-2 {
  width: 150px;
  height: 150px;
  bottom: -75px;
  left: -75px;
  animation-delay: 2s;
}

.circle-3 {
  width: 100px;
  height: 100px;
  top: 50%;
  left: 10%;
  animation-delay: 4s;
}

@keyframes float {
  0%, 100% { transform: translateY(0px) rotate(0deg); }
  50% { transform: translateY(-20px) rotate(180deg); }
}

.login-content {
  display: flex;
  width: 1000px;
  height: 600px;
  background: white;
  border-radius: 20px;
  box-shadow: 0 25px 50px rgba(0, 0, 0, 0.15);
  overflow: hidden;
  position: relative;
  z-index: 1;
}

/* 左侧装饰区域 */
.login-left {
  flex: 1;
  background: linear-gradient(135deg, #3730a3 0%, #5b21b6 100%);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 60px 40px;
  color: white;
  position: relative;
}

.login-left::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><defs><pattern id="grain" width="100" height="100" patternUnits="userSpaceOnUse"><circle cx="50" cy="50" r="1" fill="white" opacity="0.1"/></pattern></defs><rect width="100" height="100" fill="url(%23grain)"/></svg>');
  opacity: 0.3;
}

.brand-section {
  text-align: center;
  margin-bottom: 60px;
  position: relative;
  z-index: 1;
}

.logo {
  width: 80px;
  height: 80px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 20px;
  backdrop-filter: blur(10px);
}

.logo i {
  font-size: 40px;
  color: white;
}

.brand-title {
  font-size: 28px;
  font-weight: 600;
  margin-bottom: 10px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.brand-subtitle {
  font-size: 16px;
  opacity: 0.9;
  margin: 0;
}

.features {
  position: relative;
  z-index: 1;
}

.feature-item {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  font-size: 16px;
}

.feature-item i {
  margin-right: 12px;
  font-size: 18px;
  color: #4ade80;
}

/* 右侧登录表单 */
.login-right {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
}

.login-form-container {
  width: 100%;
  max-width: 400px;
}

.form-header {
  text-align: center;
  margin-bottom: 40px;
}

.form-header h2 {
  font-size: 28px;
  font-weight: 600;
  color: #1f2937;
  margin-bottom: 8px;
}

.form-header p {
  color: #6b7280;
  font-size: 16px;
  margin: 0;
}

.login-form {
  margin-bottom: 30px;
}

.custom-input {
  border-radius: 12px;
  border: 2px solid #e5e7eb;
  transition: all 0.3s ease;
}

.custom-input:hover {
  border-color: #4f46e5;
}

.custom-input:focus-within {
  border-color: #4f46e5;
  box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.1);
}

.captcha-container {
  display: flex;
  align-items: center;
  gap: 12px;
}

.captcha-input {
  flex: 1;
}

.captcha-image-container {
  width: 120px;
  height: 40px;
  border: 2px solid #e5e7eb;
  border-radius: 12px;
  overflow: hidden;
  cursor: pointer;
  transition: all 0.3s ease;
}

.captcha-image-container:hover {
  border-color: #4f46e5;
  transform: scale(1.02);
}

.captcha-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.captcha-button {
  border-radius: 12px;
  border: 2px solid #e5e7eb;
  height: 40px;
  font-size: 12px;
}

.form-options {
  display: flex;
  justify-content: flex-start;
  align-items: center;
}

.remember-checkbox {
  color: #6b7280;
}

.login-button {
  width: 100%;
  height: 48px;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
  border: none;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.login-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 10px 25px rgba(79, 70, 229, 0.3);
}

.login-button:active {
  transform: translateY(0);
}

.form-footer {
  text-align: center;
  margin-top: 20px;
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .login-content {
    width: 90%;
    max-width: 800px;
    height: auto;
    min-height: 500px;
  }
  
  .login-left {
    padding: 40px 30px;
  }
  
  .brand-title {
    font-size: 24px;
  }
  
  .brand-subtitle {
    font-size: 14px;
  }
}

@media (max-width: 768px) {
  .login-content {
    flex-direction: column;
    width: 95%;
    height: auto;
  }
  
  .login-left {
    padding: 30px 20px;
  }
  
  .login-right {
    padding: 30px 20px;
  }
  
  .brand-title {
    font-size: 20px;
  }
  
  .form-header h2 {
    font-size: 24px;
  }
}

/* 动画效果 */
.login-form-container {
  animation: slideInUp 0.6s ease-out;
}

@keyframes slideInUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 输入框焦点效果 */
:deep(.el-input__wrapper) {
  border-radius: 12px !important;
  border: 2px solid #e5e7eb !important;
  transition: all 0.3s ease !important;
}

:deep(.el-input__wrapper:hover) {
  border-color: #4f46e5 !important;
}

:deep(.el-input__wrapper.is-focus) {
  border-color: #4f46e5 !important;
  box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.1) !important;
}

/* 按钮样式优化 */
:deep(.el-button--primary) {
  background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%) !important;
  border: none !important;
  border-radius: 12px !important;
}

:deep(.el-button--primary:hover) {
  background: linear-gradient(135deg, #4338ca 0%, #6d28d9 100%) !important;
  transform: translateY(-2px) !important;
  box-shadow: 0 10px 25px rgba(79, 70, 229, 0.3) !important;
}

/* 复选框样式优化 */
:deep(.el-checkbox__input.is-checked .el-checkbox__inner) {
  background-color: #4f46e5 !important;
  border-color: #4f46e5 !important;
}

:deep(.el-checkbox__input.is-checked + .el-checkbox__label) {
  color: #4f46e5 !important;
}
</style> 